package com.yc.study.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yc.study.entity.Reply;
import com.yc.study.service.IReplyService;
import com.yc.study.service.IUserService;
import com.yc.study.vo.ReplyVo;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.List;

/**
 * 前端控制器
 *
 * @author 王妍
 * @since 2022-02-20
 */
@RestController
@RequestMapping("/study/reply")
@AllArgsConstructor
public class ReplyController {

  private final IReplyService replyService;
  private final IUserService userService;

  @PostMapping
  public void save(Principal principal, @RequestBody Reply reply) {
    final String teacherName = principal.getName();
    reply.setTeacherId(userService.loadUserByUserName(teacherName).getId());
    replyService.save(reply);
  }

  @ApiOperation("获取回复信息")
  @GetMapping("/answer/{answerId}")
  public String getReplyByAnswer(@PathVariable("answerId") Long answerId) {
    return replyService
        .list(new LambdaQueryWrapper<Reply>().eq(Reply::getAnswerId, answerId))
        .stream()
        .findFirst()
        .map(Reply::getContent)
        .orElse("");
  }
}
